package com.example.liuyuhan

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.io.File

class HistoryActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView
    private lateinit var primeAdapter: PrimeAdapter
    private val primeResults = mutableListOf<Int>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history)

        val back: Button = findViewById(R.id.back)
        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)

        // 从文件加载历史数据
        loadHistory()

        // 设置适配器
        primeAdapter = PrimeAdapter(primeResults)
        recyclerView.adapter = primeAdapter

        back.setOnClickListener {
            val intent = Intent(this, FirstActivity::class.java)
            startActivity(intent)
        }
    }

    private fun loadHistory() {
        val file = File(filesDir, "history.txt")
        if (file.exists()) {
            val history = file.readText().split(",").mapNotNull { it.toIntOrNull() }
            primeResults.addAll(history)
        }
    }
}
